
// sound-card-data-acquisitionDlg.cpp: 实现文件
//

#include "stdafx.h"
#include "sound-card-data-acquisition.h"
#include "sound-card-data-acquisitionDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CsoundcarddataacquisitionDlg 对话框



CsoundcarddataacquisitionDlg::CsoundcarddataacquisitionDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_SOUNDCARDDATAACQUISITION_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CsoundcarddataacquisitionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CsoundcarddataacquisitionDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CAPTURE, &CsoundcarddataacquisitionDlg::OnBnClickedCapture)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDOK, &CsoundcarddataacquisitionDlg::OnBnClickedOk)
END_MESSAGE_MAP()


// CsoundcarddataacquisitionDlg 消息处理程序

BOOL CsoundcarddataacquisitionDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码


	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CsoundcarddataacquisitionDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CsoundcarddataacquisitionDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();

		//自定义绘制内容
		
		

	}
	
	//drawWaveform();
	

	

}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CsoundcarddataacquisitionDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}




void CsoundcarddataacquisitionDlg::drawWaveform()
{
	m_pPanel = GetDlgItem(IDC_PANEL);//获得静态窗口对象指针
	m_pPanel2 = GetDlgItem(IDC_PANEL2);
	m_pPanelFFT = GetDlgItem(IDC_PANEL_FFT);
	
	//清屏
	m_pPanel->ShowWindow(FALSE);
	m_pPanel->ShowWindow(TRUE);

	m_pPanel2->ShowWindow(FALSE);
	m_pPanel2->ShowWindow(TRUE);

	m_pPanelFFT->ShowWindow(FALSE);
	m_pPanelFFT->ShowWindow(TRUE);

	//获取控件区域
	CRect rect, rect2, rect_fft;
	m_pPanel->GetClientRect(&rect);
	m_pPanel2->GetClientRect(&rect2);
	m_pPanelFFT->GetClientRect(&rect_fft);
	

	//获取控件画笔
	CDC* pDC = m_pPanel->GetDC();
	CDC* pDC2 = m_pPanel2->GetDC();
	CDC* pDC_fft = m_pPanelFFT->GetDC();

	//绘制原始采样数据
	m_pPanel->UpdateWindow();
	//m_recorder.RecordAudioStream();
	//m_recorder.refreshRecorder();
	//m_recorder.drawWaveform(pDC, rect,m_recorder.NoFiltering());
	m_recorder.joinData(m_recorder.captureData(),m_recorder.getDefaultCaptureNum());
	double k=m_recorder.drawWaveform(pDC, rect, m_recorder.NoFiltering(),0);

	//绘制滤波后数据
	//m_recorder.LimiterFiltering();
	m_pPanel2->UpdateWindow();
	//m_recorder.drawWaveform(pDC2, rect2,m_recorder.LimiterFiltering(m_recorder.NoFiltering()));//限幅滤波算法
	//m_recorder.drawWaveform(pDC2, rect2,m_recorder.Mean_Filtering(m_recorder.NoFiltering()));  //均值滤波算法
	//m_recorder.drawWaveform(pDC2, rect2, m_recorder.Mean_Filtering(m_recorder.LimiterFiltering(m_recorder.NoFiltering())));//限幅均值滤波算法
	//m_recorder.drawWaveform(pDC2, rect2,m_recorder.Median_Filtering(m_recorder.NoFiltering()));  //中值滤波算法
	m_recorder.drawWaveform(pDC2, rect2, m_recorder.Mean_Filtering(m_recorder.LimiterFiltering(m_recorder.NoFiltering())),k);//限幅均值滤波算法

	//绘制FFT后数据
	m_pPanelFFT->UpdateWindow();
	m_recorder.drawFrequency(pDC_fft, rect_fft, m_recorder.NoFiltering());

	ReleaseDC(pDC);
	ReleaseDC(pDC2);
	ReleaseDC(pDC_fft);
}

void CsoundcarddataacquisitionDlg::showAnalysis()//数值分析，该函数为静态函数
{
	m_pPanelNum = GetDlgItem(IDC_STATIC1);

	m_pPanelNum->ShowWindow(FALSE);
	m_pPanelNum->ShowWindow(TRUE);

	CRect rect_num;
	m_pPanelNum->GetClientRect(&rect_num);

	CDC* pDC_num = m_pPanelNum->GetDC();

	//显示数值分析
	m_pPanelNum->UpdateWindow();
	m_recorder.numericalAnalysis(pDC_num, rect_num);

	ReleaseDC(pDC_num);
}


void CsoundcarddataacquisitionDlg::OnBnClickedCapture()
{
	m_recorder.play = m_recorder.play ? FALSE : TRUE;
	if(m_recorder.play)
	{ 
		GetDlgItem(IDC_CAPTURE)->SetWindowTextW(_T("停止"));
		SetTimer(1, 500, NULL);//采样定时器
		drawWaveform();
		//showAnalysis();
	}
	else
	{
		GetDlgItem(IDC_CAPTURE)->SetWindowTextW(_T("采样"));
		KillTimer(1);
	}
}


void CsoundcarddataacquisitionDlg::OnTimer(UINT_PTR nIDEvent)
{
	switch (nIDEvent)
	{
	case 1://采样定时器
	{
		drawWaveform();
		break;
	}
	default:
		break;
	}
	CDialogEx::OnTimer(nIDEvent);
}


void CsoundcarddataacquisitionDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialogEx::OnOK();
}
